﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Admin
{
    public partial class QL_Album : System.Web.UI.Page
    {
        private B_Album albumService = new B_Album();
        B_Insert_Update_Delete insertupdate = new B_Insert_Update_Delete();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRepeater();
            }
        }

        private void BindMenu()
        {
            ddlMenu.Items.Clear();
        }

        private void LoadAlbum(int albumId)
        {
            var album = albumService.GetByPK(albumId);

            if (album != null)
            {
                // Album name
                txtAlbumName.Text = album.AlbumName;

                // Album image
                if (!string.IsNullOrEmpty(album.ImageUrl))
                    imgAvatar.ImageUrl = album.ImageUrl;
                else
                    imgAvatar.ImageUrl = "/images/noavatar.png";

                // Menu
                BindMenu();
                if (album.MenuID > 0)
                    ddlMenu.Items.FindByValue(album.MenuID.ToString()).Selected = true;

                // islock
                if (album.IsLock)
                    rdbLockYes.Checked = true;
                else if (!album.IsLock)
                    rdbLockNo.Checked = true;

                // issystem
                if (album.IsSystem)
                    rdbSystemYes.Checked = true;
                else if (!album.IsSystem)
                    rdbSystemNo.Checked = true;
            }
        }

        private void SaveAlbum(int? albumId)
        {
            var album = new M_Album();
            album.AlbumName = txtAlbumName.Text;
            if (!string.IsNullOrEmpty(ddlMenu.SelectedValue))
                album.MenuID = Convert.ToInt32(ddlMenu.SelectedValue);

            if (rdbLockYes.Checked)
                album.IsLock = true;
            else if (rdbLockNo.Checked)
                album.IsLock = false;

            if (rdbSystemYes.Checked)
                album.IsSystem = true;
            else if (rdbSystemNo.Checked)
                album.IsSystem = false;

            if (!string.IsNullOrEmpty(imgAvatar.ImageUrl))
                album.ImageUrl = imgAvatar.ImageUrl;

            if (!albumId.HasValue)
            {
                album.CreateDate = DateTime.Now;
                insertupdate.insert("Album", "AlbumID", album);
            }
            else
            {
                if (album.CreateDate < DateTime.Now.AddYears(-100))
                    album.CreateDate = DateTime.Now;
                album.AlbumID = Convert.ToInt32(albumId);
                insertupdate.update("Album", "AlbumID", album, album.AlbumID);
            }

            BindRepeater();
            ClearForm();
            pnlAdd.Visible = false;
            pnlDisplay.Visible = true;
        }

        private void BindRepeater()
        {
            var lstAlbum = albumService.GetAlls();
            rptAlbums.DataSource = lstAlbum;
            rptAlbums.DataBind();
        }

        private void ClearForm()
        {
            txtAlbumName.Text = "";
            imgAvatar.ImageUrl = "";
            ddlMenu.Items.Clear();
            rdbLockNo.Checked = false;
            rdbLockYes.Checked = false;
            rdbSystemNo.Checked = false;
            rdbSystemYes.Checked = false;
        }

        protected void BtnSelectImageClick(object sender, EventArgs e)
        {
            if (uploadAvatar.HasFile)
            {
                var fileName = Session["Username"].ToString() + DateTime.Now.ToString("yyyyMMddHHssfff") + "_" + uploadAvatar.FileName;
                var filePath = Server.MapPath("/FilesUpload/" + fileName);
                uploadAvatar.SaveAs(filePath);
                imgAvatar.ImageUrl = "/FilesUpload/" + fileName;
            }
        }


        #region Grid command

        protected void RptAlbumsItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var imgAvatar = (Image)e.Item.FindControl("imgAvatar");
            if (string.IsNullOrEmpty(imgAvatar.ImageUrl))
                imgAvatar.ImageUrl = "/images/noavatar.png";
        }

        protected void RptAlbumsItemCommand(object source, RepeaterCommandEventArgs e)
        {

            var albumId = Convert.ToInt32(e.CommandArgument);
            // thực hiện update 1 album
            if (e.CommandName == "update")
            {
                LoadAlbum(albumId);
                pnlAdd.Visible = true;
                pnlDisplay.Visible = false;
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                btnEdit.CommandArgument = albumId.ToString();
            }

            // thực hiện xóa 1 album
            if (e.CommandName == "delete")
            {
                var album = albumService.GetByPK(albumId);
                insertupdate.delete("Album", "AlbumID", album, albumId);
                BindRepeater();
            }

        }

        #endregion

        #region CRU command

        // Hiển thị panel thêm mới
        protected void BtnNewClick(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            pnlDisplay.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = false;
        }

        // Thực hiện thêm mới 1 album vào DB
        protected void BtnAddClick(object sender, EventArgs e)
        {
            SaveAlbum(null);
        }

        // Thực hiện update thông tin của 1 album
        protected void BtnUpdateClick(object sender, EventArgs e)
        {
            SaveAlbum(Convert.ToInt32(btnEdit.CommandArgument));
        }

        // Thực hiện hủy việc thêm mới/update
        protected void BtnCancelClick(object sender, EventArgs e)
        {
            ClearForm();
            pnlAdd.Visible = false;
            pnlDisplay.Visible = false;
        }
        #endregion
    }
}